package com.dc.ai.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.jwt.JWTPayload;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.ai.converter.SysUserConverter;
import com.dc.ai.entity.dto.UserAuthInfo;
import com.dc.ai.entity.form.ForgetPasswordForm;
import com.dc.ai.entity.form.UserForm;
import com.dc.ai.entity.form.UserInfoForm;
import com.dc.ai.entity.po.LoginResult;
import com.dc.ai.entity.po.SysUser;
import com.dc.ai.mapper.SysUserMapper;
import com.dc.ai.security.utils.SecurityUtils;
import com.dc.ai.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {


    private final PasswordEncoder passwordEncoder;
    private final SysUserConverter userConverter;

    @Override
    public UserAuthInfo getUserAuthInfo(String username) {
        UserAuthInfo userAuthInfo = this.baseMapper.getUserAuthInfo(username);

        return userAuthInfo;
    }

    @Override
    public boolean saveUser(UserForm userForm) {

        String username = userForm.getUsername();
        long count = this.count(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
        Assert.isTrue(count == 0, "用户名已存在");

        // 实体转换 form->entity
        SysUser entity = userConverter.form2Entity(userForm);

        entity.setNickname(userForm.getUsername());

        if (StrUtil.isNotBlank(userForm.getPassword())) {
            // 设置默认加密密码
            String defaultEncryptPwd = passwordEncoder.encode(userForm.getPassword());
            entity.setPassword(defaultEncryptPwd);
        }

        return this.save(entity);

    }

    @Override
    public boolean updateUser(UserInfoForm userInfoForm) {
        // form -> entity
        SysUser entity = userConverter.userInfoForm2Entity(userInfoForm);
        entity.setId(SecurityUtils.getUserId());
        // 修改用户
        boolean result = this.updateById(entity);
        return result;
    }

    @Override
    public boolean removeUser(String userId) {
        return this.removeById(userId);
    }

    @Override
    public boolean forgetPassword(ForgetPasswordForm forgetPasswordForm) {
        //手机号未注册
        Assert.isTrue(this.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername,forgetPasswordForm.getUsername())) != null,"当前用户不存在");
        Assert.isTrue(forgetPasswordForm.getPassword().equals(forgetPasswordForm.getRePassword()),"两次密码不一致");
        return this.update(new LambdaUpdateWrapper<SysUser>()
                .eq(SysUser::getUsername, forgetPasswordForm.getUsername())
                .set(SysUser::getPassword, passwordEncoder.encode(forgetPasswordForm.getRePassword()))
        );
    }

    @Override
    public boolean setPassword(String userId, String password) {
        return this.update(new LambdaUpdateWrapper<SysUser>()
                .eq(SysUser::getId, userId)
                .set(SysUser::getPassword, passwordEncoder.encode(password))
        );
    }

}
